﻿using AppSocketModel;
using AppSocketService.Account;
using BCGPSCommon;
using BCGPSCommon;
using BCGPSMongoData;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace AppSocketService.SendCmd
{
    public class SimCardYEHistoryService : ServiceBase,ISimCardYEHistoryService
    {
        private MongoDBCommand<SimCardYEDto> mongoCommand = new MongoDBCommand<SimCardYEDto>("CmdSimCardYEHistory");
        private readonly IRedisService redisService;
        private readonly AccountDto CurrentAccount;
        public SimCardYEHistoryService(IClaimsAccessor claimsAccesso, IRedisService redisService, IAccountService accountService, ISystemAccountService systemAccountService) : base(claimsAccesso)
        {
            this.redisService = redisService;
            if (claimsAccesso.AccountRole == AccountRole.SYSTEM)
                CurrentAccount = systemAccountService.GetCurrentAccount().AsAccountDto();
            else
                CurrentAccount = accountService.GetCurrentAccount();
        }

        public bool SetCmdYE(string imei)
        {
            if (string.IsNullOrWhiteSpace(imei) || imei.Length < 15)
                throw new ArgumentException("请输入正确的IMEI编号!");

            var dto = mongoCommand.SelectDocumentsAsync(s => s.IMEI == imei && s.CmdCodeStr == "YE" && s.CmdToward == "Send" && s.CmdDateTime > DateTime.Now.AddMonths(-1)).GetAwaiter().GetResult().OrderBy(s => s.CmdDateTime).LastOrDefault();

            if (dto != null)
            {
                if ((DateTime.Now - dto.CmdDateTime).TotalSeconds < 60)
                    throw new ArgumentException("发送过于频繁,请一分钟后再试!");
            }

            SimCardYEDto simCardYE = new SimCardYEDto();
            simCardYE.CmdDateTime = MongoDBDateTime.GetNowUtc();
            simCardYE.IMEI = imei;
            simCardYE.CmdCode = ((int)GpsCmdCode.YE).ToString();
            simCardYE.CmdCodeStr = GpsCmdCode.YE.ToString();
            simCardYE.CmdParamer = GeneralContainer.configuration["YE"];
            if (simCardYE.CmdParamer == null)
                throw new Exception("没有配置余额命令!");
            simCardYE.CmdToward = "Send";
            simCardYE.CompanyId = CurrentAccount.CompanyId;
            simCardYE.AccountId = CurrentAccount.AccountId;
            simCardYE.AccountCode = CurrentAccount.AccountCode;
            simCardYE.AccountRole = CurrentAccount.AccountRole;

            mongoCommand.InsertDocumentAsync(simCardYE).GetAwaiter().GetResult();

            redisService.AddAsync(BCGPSCommon.GeneralContainer.RedisNameSpaceIMEISendCmd + imei,"$CMD$1$" + simCardYE.CmdParamer + "$");

            return true;
        }
         
        public PageableList<SimCardYEDto > GetList(string imei,DateTime start,DateTime end, bool desc, int index, int size)
        {
            index = index > 0 ? index - 1 : 0;
            if (end.Hour == 0 && end.Minute == 0 && end.Second == 0)
                end = end.AddDays(1);
            start = MongoDBDateTime.GetUtc(start);
            end = MongoDBDateTime.GetUtc(end);
            return mongoCommand.SelectDocumentsAsync(s => s.IMEI == imei && s.CmdDateTime > start && s.CmdDateTime < end, s => s.CmdDateTime, desc, index * size, size).GetAwaiter().GetResult();
        }
    }
}
